package com.sk.pay.lib

import android.app.Activity
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.text.TextUtils
import android.util.Log
import com.alipay.sdk.app.PayTask
import com.sk.pay.lib.aipay.PayResult
import com.tencent.mm.opensdk.modelpay.PayReq
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import org.json.JSONException
import org.json.JSONObject

class PaymentUtil {

    /**
     *  @time 2025/5/14  15:06
     *  @author Im_jingwei
     *  @desc 微信支付
     */
    fun wechatPay(context: Context, data: JSONObject) {
        try {
            val api = WXAPIFactory.createWXAPI(context, null)
            api.registerApp(data.getString("appid"))
            val req = PayReq()
            req.appId = data.getString("appid")
            req.partnerId = data.getString("partnerid")
            req.prepayId = data.getString("prepayid")
            req.nonceStr = data.getString("noncestr")
            req.timeStamp = data.getString("timestamp")
            req.packageValue = data.getString("package")
            req.sign = data.getString("sign")
            api.sendReq(req)
        } catch (e: JSONException) {
            e.printStackTrace()
        }
    }

    fun wxTest(context: Context)
    {
        try {
//            val api = WXAPIFactory.createWXAPI(context, null)
//            api.registerApp("wx04d5cdc192e77d67")
            val api = WXAPIFactory.createWXAPI(context, "wx04d5cdc192e77d67", false);
            val request = PayReq();
            request.appId = "wx04d5cdc192e77d67";
            request.partnerId = "1900000109";
            request.prepayId= "1101000000140415649af9fc314aa427";
            request.packageValue = "Sign=WXPay";
            request.nonceStr= "1101000000140429eb40476f8896f4c9";
            request.timeStamp= "1398746574";
            request.sign= "oR9d8PuhnIc+YZ8cBHFCwfgpaK9gd7vaRvkYD7rthRAZ\\/X+QBhcCYL21N7cHCTUxbQ+EAt6Uy+lwSN22f5YZvI45MLko8Pfso0jm46v5hqcVwrk6uddkGuT+Cdvu4WBqDzaDjnNa5UK3GfE1Wfl2gHxIIY5lLdUgWFts17D4WuolLLkiFZV+JSHMvH7eaLdT9N5GBovBwu5yYKUR7skR8Fu+LozcSqQixnlEZUfyE55feLOQTUYzLmR9pNtPbPsu6WVhbNHMS3Ss2+AehHvz+n64GDmXxbX++IOBvm2olHu3PsOUGRwhudhVf7UcGcunXt8cqNjKNqZLhLw4jq\\/xDg==";
            api.sendReq(request);
        } catch (e: JSONException) {
            e.printStackTrace()
        }
    }



   /**
   *  @time 2025/5/14  15:42
    *  @author Im_jingwei
    *  @desc 支付宝支付
    */
    fun aliPay(context: Activity, orderInfo: String,mHandler: Handler) {
        val payRunnable = Runnable {
            val alipay = PayTask(context)
            val result = alipay.payV2(orderInfo, true)
            Log.i("msp", result.toString())
            val msg = Message()
            msg.what = PayConfig.ALIPAY_SDK_PAY_FLAG
            msg.obj = result
            mHandler.sendMessage(msg)
        }
        // 必须异步调用
        val payThread = Thread(payRunnable)
        payThread.start()
    }

    private val mHandler: Handler = object : Handler(Looper.getMainLooper()) {
        override fun handleMessage(msg: Message) {
            when (msg.what) {
                PayConfig.ALIPAY_SDK_PAY_FLAG -> {
                    val payResult = PayResult(msg.obj as Map<String?, String?>)
                    /**
                     * 对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
                     */
                    val resultInfo = payResult.result // 同步返回需要验证的信息
                    val resultStatus = payResult.resultStatus
                    // 判断resultStatus 为9000则代表支付成功
                    if (TextUtils.equals(resultStatus, "9000")) {
                        // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
//                        PayDemoActivity.showAlert(this@PayDemoActivity, getString(R.string.pay_success) + payResult)
                    } else {
                        // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
//                        PayDemoActivity.showAlert(this@PayDemoActivity, getString(R.string.pay_failed) + payResult)
                    }
                }
                else -> {}
            }
        }
    }

}